package com.hiultra.archive.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.archive.domain.ArchiveArchArea;
import com.hiultra.archive.service.IArchiveArchAreaService;
import com.hiultra.common.utils.poi.ExcelUtil;
import com.hiultra.common.core.page.TableDataInfo;

/**
 * 区Controller
 *
 * @author paidaxing
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/archive/ArchiveArchArea")
public class ArchiveArchAreaController extends BaseController {
    @Autowired
    private IArchiveArchAreaService archiveArchAreaService;

    /**
     * 查询区列表
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchiveArchArea archiveArchArea) throws JsonProcessingException {
        startPage();
        List<ArchiveArchArea> list = archiveArchAreaService.selectArchiveArchAreaList(archiveArchArea);
        // 创建一个代码和描述的映射字典
        Map<String, String> codeDescriptionMap = new HashMap<>();
        codeDescriptionMap.put("01", "高压用电户档案");
        codeDescriptionMap.put("02", "低压非居用电户档案");
        codeDescriptionMap.put("03", "低压居民用电户档案");
        codeDescriptionMap.put("04", "高压发电户档案");
        codeDescriptionMap.put("05", "低压发电户档案");
        codeDescriptionMap.put("06", "个人客户档案");
        codeDescriptionMap.put("07", "组织客户档案");
        codeDescriptionMap.put("08", "关口档案");
        codeDescriptionMap.put("99", "其他");
        for (ArchiveArchArea item : list) {
            // 从数据库获取的值
            String databaseValue = item.getArchType();

            // 将获取的值以逗号分隔拆分成数组
            String[] codes = databaseValue.split(",");

            // 构建描述字符串
            StringBuilder descriptionBuilder = new StringBuilder();
            for (String code : codes) {
                if (codeDescriptionMap.containsKey(code)) {
                    descriptionBuilder.append(codeDescriptionMap.get(code)).append(", ");
                }
            }

            // 删除最后一个逗号和空格
            String descriptions = descriptionBuilder.toString();
            if (descriptions.endsWith(", ")) {
                descriptions = descriptions.substring(0, descriptions.length() - 2);
            }

            item.setArchType(descriptions);
        }
        return getDataTable(list);
    }

    /**
     * 导出区列表
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:export')")
    @Log(title = "区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveArchArea archiveArchArea) throws JsonProcessingException {
        List<ArchiveArchArea> list = archiveArchAreaService.selectArchiveArchAreaList(archiveArchArea);
        ExcelUtil<ArchiveArchArea> util = new ExcelUtil<ArchiveArchArea>(ArchiveArchArea.class);
        util.exportExcel(response, list, "区数据");
    }

    /**
     * 获取区详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(archiveArchAreaService.selectArchiveArchAreaById(id));
    }

    /**
     * 新增区
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:add')")
    @Log(title = "区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveArchArea archiveArchArea) {
        return toAjax(archiveArchAreaService.insertArchiveArchArea(archiveArchArea));
    }

    /**
     * 修改区
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:edit')")
    @Log(title = "区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveArchArea archiveArchArea) {
        return toAjax(archiveArchAreaService.updateArchiveArchArea(archiveArchArea));
    }

    /**
     * 删除区
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:remove')")
    @Log(title = "区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(archiveArchAreaService.deleteArchiveArchAreaByIds(ids));
    }

    /**
     * 查询区列表(无分页)
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveArchArea:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ArchiveArchArea archiveArchArea) throws JsonProcessingException {
        List<ArchiveArchArea> list = archiveArchAreaService.selectArchiveArchAreaList(archiveArchArea);
        return getDataTable(list);
    }
}
